import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:app/src/config/app_data_config.dart';
import 'package:app/src/http/bean/http_data.dart';
import 'package:app/src/http/sign.dart';
import 'package:app/src/http/url.dart';

import '../bean/clockin/attendances.dart';

///服务器地址，密钥
class ConfigModel {
  ///获取App密钥
  ///baseUrl 传入输入的域名，通过密钥接口检查域名是否正确。
  static void getAppCode(
      BuildContext context,
      String baseUrl,
      String customCode,
      bool isEnableLoad,
      ResponseCallbackWithBool? callback) {
    var url = Url.baseurl(baseUrl: baseUrl) + Url.sysgetappcode;
    // print("密钥url:$url");
    KOhttp(url)
      ..isEnableLoad(isEnableLoad, context)
      // ..onStart(() {
      //   print('开始');
      // })
      ..onError((code, error) {
        // print('密钥错误:\t$code\t$error');
        callback?.call(false);
      })
      ..onSuccess((result) {
        // print('密钥成功:\n$result');
        if (StringUtils.isEmpty(result)) {
          callback?.call(false);
        }
      })
      // ..onFinish(() {
      //   print('结束');
      // })
      ..contentType = ContentType.form
      ..isAllowedRepeat = true
      ..isEnableLog = AppDataConfig.isEnableLog
      ..postASync<AppHttpResult<String, String>>(AppHttpResult()
            ..Data = ""
            ..t2 = "")
          ?.then((result) {
        //print(result.toString());
        if (result.isSuccessForCode()) {
          //域名正确,保存服务器地址
          AppDataConfig.setAdress(baseUrl);
          //保存密钥
          AppDataConfig.saveAppCode(result.Code, customCode, result.Num);
          callback?.call(true);
        } else {
          callback?.call(false);
        }
      });
  }

  ///获取工厂列表
  static void getFactorygetlist(BuildContext context, bool isEnableLoad,
      ResponseCallbackWithT_<AppHttpResult<List<Factory>, Factory>>? callback) {
    var url = Url.baseurl() + Url.sysfactorygetlist;
    // print("工厂url$url");
    CommonData commonData = CommonData();
    var queryParameters = Map<String, dynamic>();
    var data = commonData.jsonEncodeToJson();
    var token = AppDataConfig.getToken();
    queryParameters['data'] = data;
    var sign = getSign(queryParameters);
    //print('签名：$sign');
    KOhttp(url)
      ..isEnableLoad(isEnableLoad, context)
      // ..onStart(() {
      //   print('开始');
      // })
      ..onError((code, error) {
        print('工厂获取错误:\t$code\t$error');
        callback?.call(null);
      })
      ..onSuccess((result) {
        // print('工厂获取成功:\n$result');
        if (StringUtils.isEmpty(result)) {
          callback?.call(null);
        }
      })
      ..onFinish(() {
        // print('结束');
      })
      ..isCache(false)
      ..addHeader('token', token)
      ..addFormData('data', data)
      ..addFormData('sign', sign)
      ..contentType = ContentType.form
      ..isAllowedRepeat = true
      ..isEnableLog = false //true //AppDataConfig.isEnableLog
      // ..postASync(AppHttpResult<String,String>()
      //   ..Data = ""
      //   ..t2 = "")
      //     ?.then((result) {
      //   print("返回："+result.toString());
      // });
      ..postASync<AppHttpResult<List<Factory>, Factory>>(AppHttpResult()
            ..Data = <Factory>[]
            ..t2 = Factory())
          ?.then((result) {
        // print("返回：" + result.toString());
        callback?.call(result);
      });
  }

  ///加班审核：排班，获取班组
  static void getSchedulingsWithGroupBcInfo(
      BuildContext context,
      bool isEnableLoad,
      String? FactoryId,
      ResponseCallbackWithT_<AppHttpResult<List<MyGroupBcInfo>, MyGroupBcInfo>>?
          callback) {
    var url = Url.baseurl() + Url.getMyGroupBcInfo;
    CommonData commonData = CommonData();
    if (FactoryId != null) {
      commonData.factoryId = FactoryId; //fixme 工厂id
    }
    var queryParameters = Map<String, dynamic>();
    var data = commonData.jsonEncodeToJson();
    var token = AppDataConfig.getToken();
    queryParameters['data'] = data;
    //print('提交参数：$data');
    var sign = getSign(queryParameters);
    //print('签名：$sign');
    KOhttp(url)
      ..isEnableLoad(isEnableLoad, context)
      // ..onStart(() {
      //   print('开始');
      // })
      ..onError((code, error) {
        // print('错误:\t$code\t$error');
        callback?.call(null);
      })
      ..onSuccess((result) {
        //print('成功:\n$result');
        if (StringUtils.isEmpty(result)) {
          callback?.call(null);
        }
      })
      ..onFinish(() {
        // print('结束');
      })
      ..isCache(false)
      ..addHeader('token', token)
      ..addFormData('data', data)
      ..addFormData('sign', sign)
      ..contentType = ContentType.form
      ..isEnableLog = AppDataConfig.isEnableLog
      ..postASync<AppHttpResult<List<MyGroupBcInfo>, MyGroupBcInfo>>(
              AppHttpResult()
                ..Data = <MyGroupBcInfo>[]
                ..t2 = MyGroupBcInfo())
          ?.then((result) {
        // print("返回："+result.toString());
        callback?.call(result);
      });
  }
}
